package com.ruomeng.robot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;


import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
@SpringBootApplication
@MapperScan("com.ruomeng.robot.*")
public class RobotApplication {

	public static void main(String[] args) {
		SpringApplication.run(RobotApplication.class, args);
	}

	 @Bean
	    public HttpMessageConverters fastJsonHttpMessageConverters() {
	       FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
	       FastJsonConfig fastJsonConfig = new FastJsonConfig();
	       fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
	       fastConverter.setFastJsonConfig(fastJsonConfig);
	       HttpMessageConverter<?> converter = fastConverter;
	       return new HttpMessageConverters(converter);
	    }

	//DataSource配置
	    @Bean
	    @ConfigurationProperties(prefix="spring.datasource")
	    public DataSource dataSource() {
	        return new org.apache.tomcat.jdbc.pool.DataSource();
	    }
	 
	    //提供SqlSeesion
	    @Bean
	    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
	 
	        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
	        sqlSessionFactoryBean.setDataSource(dataSource());
	 
	        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
	 
	        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mapper/*.xml"));
	 
	        return sqlSessionFactoryBean.getObject();
	    }
	 
	    @Bean
	    public PlatformTransactionManager transactionManager() {
	        return new DataSourceTransactionManager(dataSource());
	    }
	 
	 
}
